package com.yx.cloud.utils;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.iot.common.utils.LogUtils;
import com.yx.cloud.BuildConfig;
import com.yx.cloud.R;
import com.yx.cloud.bean.CountryCodeBean;
import com.yx.cloud.utils.LanguageUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by Administrator on 2018/12/28.
 */

public class CountryCodeUtils {
    private static final String TAG = "CountryCodeUtils";
    private static ArrayList<CountryCodeBean> countries = null;

    /**
     * 自动获取当前国家?
     * <p>
     * 亿赛通DLP数据泄露防护系统
     */
    public static String getCountryZipCode(Context context) {
        String CountryID;
        String CountryZipCode = BuildConfig.DEFAULT_COUNTRY_CODE;
//        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        CountryID = BuildConfig.DEFAULT_COUNTRY_CODE;
//        Log.d("ss", "CountryID-11111-->>>" + CountryID);
        String[] rl = context.getResources().getStringArray(R.array.CountryCodes);
        for (int i = 0; i < rl.length; i++) {
//            Log.d("ss", "CountryID--->>>" + rl[i].toString());
            String[] g = rl[i].split(",");
            if (g[1].trim().equals(CountryID.trim())) {
                CountryZipCode = g[0];
                break;
            }
        }
        return CountryZipCode;
    }


    public static ArrayList<CountryCodeBean> getCountryCodeList(Context ctx) {
        LogUtils.i("CountryID- getCountryCodeList-->>>");
//        if (countries != null)
//            return countries;
        countries = new ArrayList<>();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(ctx.getResources().getAssets().open("anim/json/" + "country.json")));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null)
                sb.append(line);
            br.close();
            JSONArray ja = new JSONArray(sb.toString());
            String key = getKey(ctx);
            //LogUtils.i("key : " + key);
            for (int i = 0; i < ja.length(); i++) {
                JSONObject jo = ja.getJSONObject(i);
                int flag = 0;
                String locale = jo.getString("alias_name");
                // LogUtils.i("alias_name : " + locale);
                if (!TextUtils.isEmpty(locale)) {
                    flag = ctx.getResources().getIdentifier("flag_" + locale.toLowerCase(), "drawable", ctx.getPackageName());
                }
                CountryCodeBean countryCodeBean = new CountryCodeBean();
                countryCodeBean.setCode(jo.getInt("country_code"));
                //LogUtils.i("Name : " + jo.getString(key));
                countryCodeBean.setName(jo.getString(key));
                countryCodeBean.setLocale(locale);
                countryCodeBean.setFlag(flag);
                countryCodeBean.setZh(jo.getString("chinese_name"));
                countryCodeBean.setEn(jo.getString("english_name"));
                countryCodeBean.setKo(jo.getString("ko_name"));
                countries.add(countryCodeBean);
            }

            Log.i(TAG, countries.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return countries;
    }

    private static String getKey(Context ctx) {
//        String country = "";
        String mLanguage = LanguageUtils.getAndsetLanguage(ctx, "选择国家码");
        String country = LanguageUtils.getSystemLanguage(ctx).toUpperCase();
        LogUtils.i("CountryID- sssss-->>>" + country + " , " + mLanguage);

        if ("ZH-CN".equalsIgnoreCase(country)) {
            return ctx.getString(R.string.ZH).equals(mLanguage) ? "chinese_name"
                    : ctx.getString(R.string.EN).equals(mLanguage) ? "english_name"
                    : ctx.getString(R.string.KO).equals(mLanguage) ? "ko_name"
                    : ctx.getString(R.string.TW).equals(mLanguage) ? "tw_name"
                    : "english_name";
        } else if ("EN-US".equalsIgnoreCase(country)) {
            return ctx.getString(R.string.ZH).equals(mLanguage) ? "chinese_name"
                    : ctx.getString(R.string.EN).equals(mLanguage) ? "english_name"
                    : ctx.getString(R.string.KO).equals(mLanguage) ? "ko_name"
                    : ctx.getString(R.string.TW).equals(mLanguage) ? "tw_name"
                    : "english_name";
        } else if ("KO-KR".equalsIgnoreCase(country)) {
            return ctx.getString(R.string.ZH).equals(mLanguage) ? "chinese_name"
                    : ctx.getString(R.string.EN).equals(mLanguage) ? "english_name"
                    : ctx.getString(R.string.KO).equals(mLanguage) ? "ko_name"
                    : ctx.getString(R.string.TW).equals(mLanguage) ? "tw_name"
                    : "english_name";
        } else if ("ZH-TW".equalsIgnoreCase(country)) {
            return ctx.getString(R.string.ZH).equals(mLanguage) ? "chinese_name"
                    : ctx.getString(R.string.EN).equals(mLanguage) ? "english_name"
                    : ctx.getString(R.string.KO).equals(mLanguage) ? "ko_name"
                    : ctx.getString(R.string.TW).equals(mLanguage) ? "tw_name"
                    : "english_name";
        } else {
            return "ZH-CN".equalsIgnoreCase(country) ? "chinese_name"
                    : "EN-US".equalsIgnoreCase(country) ? "english_name"
                    : "KO-KR".equalsIgnoreCase(country) ? "ko_name"
                    : "ZH-TW".equalsIgnoreCase(country) ? "tw_name"
                    : "english_name";
        }
    }

//    private GetCountryNameSort countryChangeUtil;
//    private CharacterParserUtil characterParserUtil;
//    /**
//     * 获取国家列表
//     */
//    private void getCountryList(Context context){
//        String[] countryList = context.getResources().getStringArray(R.array.country_code_list_ch);
//
//        for (int i = 0, length = countryList.length; i < length; i++)
//        {
//            String[] country = countryList[i].split("\\*");
//
//            String countryName = country[0];
//            String countryNumber = country[1];
//            String countrySortKey = characterParserUtil.getSelling(countryName);
//            CountrySortModel countrySortModel = new CountrySortModel(countryName, countryNumber,
//                    countrySortKey);
//            String sortLetter = countryChangeUtil.getSortLetterBySortKey(countrySortKey);
//            if (sortLetter == null)
//            {
//                sortLetter = countryChangeUtil.getSortLetterBySortKey(countryName);
//            }
//
//            countrySortModel.sortLetters = sortLetter;
//            mAllCountryList.add(countrySortModel);
//        }
//
//        Collections.sort(mAllCountryList, pinyinComparator);
//        adapter.updateListView(mAllCountryList);
//        Log.e(TAG, "changdu" + mAllCountryList.size());
//    }

}